﻿using System;

namespace Tools.AlgorithmTools
{
    public class random
    {
        // 随机变量
        private static Random ra = new Random(unchecked((int)DateTime.Now.Ticks));

        /// <summary>
        /// 生成随机数列表
        /// </summary>
        /// <param name="minValue">最小值</param>
        /// <param name="maxValue">最大值</param>
        /// <param name="num">随机个数</param>
        /// <returns>随机数列表</returns>
        public static int[] RandomInt(int minValue, int maxValue, int num)
        {
            int[] arrNum = new int[num];

            int tmp = 0;
            for (int i = 0; i <= num - 1; i++)
            {
                tmp = GetRandomInt(minValue, maxValue);
                arrNum[i] = tmp;
            }
            return arrNum;
        }

        /// <summary>
        /// 随机数列表  有最小间隙
        /// </summary>
        /// <param name="minValue">最小值</param>
        /// <param name="maxValue">最大值</param>
        /// <param name="num">个数</param>
        /// <param name="minGap">最小间隙</param>
        /// <returns>随机数列表</returns>
        public static int[] RandomIntGap(int minValue, int maxValue, int num, double minGap = 1)
        {
            int[] arrNum = new int[num];

            int tmp = 0;
            for (int i = 0; i <= num - 1; i++)
            {
                int maxRandomTimes = 1000;
                while (true)
                {
                    tmp = GetRandomInt(minValue, maxValue);
                    if (IsRepeat(arrNum, tmp, minGap) == false)
                    {
                        arrNum[i] = tmp;
                        break;
                    }

                    maxRandomTimes--;

                    if (maxRandomTimes <= 0)
                        break;
                    
                }
            }
            return arrNum;
        }

        // 获取最大最小值
        private static int GetRandomInt(int minValue, int maxValue)
        {
        	//随机取数
            int result = ra.Next(minValue, maxValue);
            return result;
        }

        // 是否重复
        private static bool IsRepeat(int[] arrNum, float tmp, double gap = 0.01)
        {
            for (int i = 0; i < arrNum.Length; i++)
            {
                if (System.Math.Abs(arrNum[i] - tmp) <= gap)
                    return true;
            }
            return false;
        }
    }

}